﻿namespace DimensionsHelper.ExcelAddIn.Core.Sql;

/// <summary>
/// 用于向前端传输和接收的 SQL 设置
/// </summary>
public class ScriptSettingsTransmission : IExcelAddInTransmission
{

    /// <summary>
    /// 表头标识符设置
    /// </summary>
    public HeaderIdentifiersTransmission Identifiers { get; set; } = new HeaderIdentifiersTransmission();

    /// <summary>
    /// ID字段名
    /// </summary>
    public string IdFieldName { get; set; } = string.Empty;

    /// <summary>
    /// 是否是文本ID
    /// </summary>
    public bool IsTextId { get; set; }

    /// <summary>
    /// SQL 脚本列表头
    /// </summary>
    public string ScriptColumnHeader { get; set; } = "SQL_SCRIPT";

    /// <summary>
    /// 额外条件的逻辑类型
    /// </summary>
    public string ExtraLogic { get; set; } = "AND";

    /// <summary>
    /// 分类值的更新类型
    /// </summary>
    public string CategoricalUpdateKind { get; set; } = "APPEND";

    /// <summary>
    /// 是否创建数据表
    /// </summary>
    public bool BuildDataTable { get; set; }

    /// <summary>
    /// 数据表的设置
    /// </summary>
    public DataTableSettingsTransmission DataTable { get; set; } = new();


    public IExcelAddInSettings ToSettings()
    {
        return new ScriptSettings
        {
            IdFieldName = IdFieldName,
            Identifiers = Identifiers.ToSettings(),
            IsTextId = IsTextId,
            ScriptColumnHeader = ScriptColumnHeader,
            ExtraLogic = ExtraLogic == "OR" ? LogicKinds.Or : LogicKinds.And,
            CategoricalUpdateKind = CategoricalUpdateKind == "RESET"
                ? CategoricalUpdateKinds.Reset : CategoricalUpdateKinds.Append,
            BuildDataTable = BuildDataTable,
            DataTable = DataTable.ToSettings()
        };
    }


    public void ToSettings(IExcelAddInSettings settings)
    {
        if (settings is not ScriptSettings scriptSettings)
        {
            return;
        }

        scriptSettings.IdFieldName = IdFieldName;
        scriptSettings.Identifiers = Identifiers.ToSettings();
        scriptSettings.IsTextId = IsTextId;
        scriptSettings.ScriptColumnHeader = ScriptColumnHeader;
        scriptSettings.ExtraLogic = ExtraLogic == "OR" ? LogicKinds.Or : LogicKinds.And;
        scriptSettings.CategoricalUpdateKind = CategoricalUpdateKind == "RESET"
                ? CategoricalUpdateKinds.Reset : CategoricalUpdateKinds.Append;
        scriptSettings.BuildDataTable = BuildDataTable;
        scriptSettings.DataTable = DataTable.ToSettings();
    }
}